package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")//设置了一个根路径
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController{
    @Autowired
    private IDepartmentService departmentService;

    //http://localhost:8080/dept/list
//    @RequestMapping("/list")
    //GET  http://localhost:8080/dept/list
//    @RequestMapping(method = RequestMethod.GET,path = "/list")  也需要记住
    //GET  http://localhost:8080/dept/list
    @GetMapping("/list")
    @ApiOperation(value = "查询所有部门" )
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /**
     * get 请求是没有请求的体 RequestBody
     * 400错 请求参数类型不匹配
     * @param query
     * @return
     */
    @PreAuthorize(name = "部门列表",value = "dept:pagelist")
    @PostMapping("/pagelist")
    @ApiOperation(value = "高级查询&分页" )
    public PageList<Department> pageQueryAll(@RequestBody DepartmentQuery query){
        return departmentService.pageQueryAll(query);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryOne(@PathVariable("id") Long id){
        return departmentService.queryOne(id);
    }

    @PutMapping("/department")
    @ApiOperation(value = "新增|修改部门 如果没有id就是新增有id就是修改" )
    @PreAuthorize(name = "部门保存",value = "dept:save")
    public AjaxResult update(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.insert(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/department/{id}")
    @ApiOperation(value = "根据id删除部门" )
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //批量删除的注解
    @PatchMapping("/department")
    @ApiOperation(value = "批量删除部门" )
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "无限极部门树" )
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
